package cn.aishua.location;

import android.content.Context;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

/**
 * It's supposed to be a finder that finds where I am or vendors around me.
 */
public class Locator
{

    private Context mContext;

    private static Locator self = null;

    private LocationClient mClient;

    private Callback mCallback;

    private BDLocation mLocation;

    private Locator(Context context)
    {
        mContext = context;
    }

    public static synchronized Locator getInstance( Context context )
    {
        if( self == null )
        {
            self = new Locator( context );
        }
        return self;
    }

    public void find( Callback cb )
    {
        Log.d( "test", "locator find" );
        mClient = new LocationClient( mContext );
        mClient.registerLocationListener( new MyLocationListenner() );
        mCallback = cb;
        setLocationOption();
        mClient.start();
    }

    public void start()
    {
        Log.d( "test", "locator find" );
        mClient = new LocationClient( mContext );
        mClient.registerLocationListener( new MyLocationListenner() );
        setLocationOption();
        mClient.start();
    }

    public void locate( Callback cb )
    {
        mCallback = cb;
        mClient.requestLocation();
    }

    private void clean()
    {
        if( mClient != null && mClient.isStarted() )
        {
            Log.d( "Locator", "stop Location Client" );
            mClient.stop();
            mClient = null;
        }
    }

    private void setLocationOption()
    {
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps( true );
        option.setCoorType( "bd09ll" );
        option.setAddrType( "all" );
        option.setScanSpan( 10000 );
        mClient.setLocOption( option );
    }

    public class MyLocationListenner
        implements BDLocationListener
    {
        public void onReceiveLocation( BDLocation arg )
        {
            if( arg == null )
            {
                //                clean();
                return;
            }

            Log.d( "Locator", "location: ( " + arg.getLatitude() + ", " + arg.getLongitude() + " )" );
            mLocation = arg;
            if( mCallback != null )
            {
                mCallback.onFindFinished( mLocation );

            }
            clean();
            return;
        }

        public void onReceivePoi( BDLocation arg )
        {
            // Currently do nothing.
        }
    }

    public BDLocation getLocation()
    {
        return mLocation;
    }

    /**
     * This is the call-back for UI to get the location information.
     * 
     * @author larry
     */
    public interface Callback
    {
        public void onFindFinished( BDLocation location );
    }

}
